
package com.joysuch.wwyt.core.support;

import com.joysuch.wwyt.core.security.ShiroUser;

import java.util.List;

/**
 * 应用上下文
 */
public abstract class Context {
    /**
     * 是否手机访问线程变量
     */
    private static ThreadLocal<Boolean> isMobileHolder = new ThreadLocal<Boolean>();

    public static void setMobile(boolean isMobile) {
        isMobileHolder.set(isMobile);
    }

    public static boolean isMobile() {
        return isMobileHolder.get() != null && isMobileHolder.get();
    }

    public static void resetMobile() {
        isMobileHolder.remove();
    }

    /**
     * 用户线程变量
     */
    private static ThreadLocal<ShiroUser> userHolder = new ThreadLocal<>();

    public static void setCurrentUser(ShiroUser user) {
        userHolder.set(user);
    }

    public static ShiroUser getCurrentUser() {
        return userHolder.get();
    }

    public static void resetCurrentUser() {
        userHolder.remove();
    }

    public static Long getCurrentUserId() {
        ShiroUser user = userHolder.get();
        if (user == null) {
            return null;
        }
        return user.getRealUserId();
    }

    public static Long getCurrentLoginAccountId() {
        return userHolder.get().getRealUserId();
    }

    public static Long getCurrentTenantId() {
        ShiroUser currentUser = getCurrentUser();
        return currentUser == null ? null : currentUser.getTenentId();
    }

    public static String getCurrentOrgCode() {
        ShiroUser currentUser = getCurrentUser();
        return currentUser == null ? null : currentUser.getCompanyCode();
    }

    public static String getCompanyCode() {
        ShiroUser currentUser = getCurrentUser();
        return currentUser == null ? null : currentUser.getCompanyCode();
    }

    public static Long getCurrentDepartId() {
        ShiroUser user = userHolder.get();
        if (user == null) {
            return null;
        }
        return user.getDepartId();
    }

    public static Long getJobId() {
        ShiroUser currentUser = getCurrentUser();
        return currentUser == null ? null : currentUser.getJobId();
    }


    public static List<Long> getRoleIds() {
        ShiroUser currentUser = getCurrentUser();
        return currentUser == null ? null : currentUser.getRoleIds();
    }

    public static String getCompanyName() {
        ShiroUser currentUser = getCurrentUser();
        return currentUser == null ? null : currentUser.getCompanyName();
    }

    public static Long getCompanyId() {
        ShiroUser currentUser = getCurrentUser();
        return currentUser == null ? null : currentUser.getCompanyId();

    }
    public static String getCurrentCode() {
        ShiroUser currentUser = getCurrentUser();
        return currentUser == null ? null : currentUser.getCode();
    }
}
